package com.edu.test.dbf;

import cn.hutool.core.bean.BeanUtil;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;

public class DbfUtil {
    private DbfUtil() {
    }

    public static List<Map<String, String>> getReader(InputStream inputStream, String charset) {
        List<Map<String, String>> maps = new ArrayList<>();
        try {
            DBFReader dbfReader = new DBFReader(inputStream);
            //设置编码
            dbfReader.setCharset(Charset.forName(charset));
            //读取列
            dbfReader.getFieldCount();
            Object[] rowValues = null;
            while ((rowValues = dbfReader.nextRecord()) != null && rowValues.length != 0) {
                int index = 0;
                Map<String, String> map = new LinkedHashMap<>();
                for (int i = 0; i < rowValues.length; i++) {
                    DBFField field = dbfReader.getField(index);
                    if (BeanUtil.isNotEmpty(rowValues[i])) {
                        map.put(field.getName().toUpperCase(), rowValues[i].toString());
                    }else {
                        map.put(field.getName().toUpperCase(), null);
                    }
                    index++;
                }
                maps.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return maps;
    }

    /**
     * @param file    文件路径
     * @param charset 编码格式
     * @return
     */
    public static List<Map<String, String>> getReader(File file, String charset) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            return getReader(fis, charset);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * (行数一致)按照原内容取出dbf文件内容
     *
     * @param path
     * @param charsetName
     * @return
     */
    public static List<List<Object>> readDbfToList(String path, String charsetName) {
        List<List<Object>> lists = new ArrayList<>();
        //首列
        List<Object> titles = new ArrayList<>();
        FileInputStream fis = null;
        //设置文件编码格式
        try {
            fis = new FileInputStream(path);
            DBFReader dbfReader = new DBFReader(fis);
            Charset charset = Charset.forName(charsetName);
            dbfReader.setCharset(charset);
            int fieldCount = dbfReader.getFieldCount();
            for (int i = 0; i < fieldCount; i++) {
                DBFField field = dbfReader.getField(i);
                titles.add(field.getName());
            }
            lists.add(titles);
            Object[] objs = null;
            while ((objs = dbfReader.nextRecord()) != null && objs.length != 0) {
                List<Object> temps = new ArrayList<>();
                for (Object obj : objs) {
                    temps.add(obj);
                }
                lists.add(temps);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return lists;
    }
}
